package com.shuqi.model.parse.parser;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import com.shuqi.model.bean.ItemInfo;
import com.shuqi.model.bean.RechargesHistosyData;
import com.shuqi.model.parse.HandlerBase;

/**
 * 充值记录解析
 * 
 * @author zhuchen
 * 
 */
public class RechargesHistoryParser extends HandlerBase {

    private RechargesHistosyData data;
    private ItemInfo itemInfo;
    private List<ItemInfo> list;

    private String currentName;

    private String hint;

    @Override
    public Object getParsedData() {
        // TODO Auto-generated method stub
        return data;
    }

    @Override
    public void startDocument() throws SAXException {
        // TODO Auto-generated method stub

        list = new ArrayList<ItemInfo>();
        super.startDocument();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes)
            throws SAXException {
        if ("hint".equals(localName)) {
            currentName = localName;
        } else if ("Recharges".equals(localName)) {
            data = new RechargesHistosyData();
            data.setPageCount(isNull(attributes, "pageCount"));
            data.setTotalCount(isNull(attributes, "totalCount"));
            data.setHint(hint);
        } else if ("item".equals(localName)) {
            itemInfo = new ItemInfo();
            try {
                String time = isNull(attributes, "date");
                itemInfo.setDate(String.valueOf(Long.parseLong(time) * 1000));
            } catch (Exception e) {
            }
            itemInfo.setType(isNull(attributes, "type"));
            itemInfo.setAmount(isNull(attributes, "amount"));
            itemInfo.setCode(isNull(attributes, "code"));
            itemInfo.setStatus(isNull(attributes, "status"));
            itemInfo.setTypeId(isNull(attributes, "typeId"));
            list.add(itemInfo);
            data.setList(list);
        }

    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        super.characters(ch, start, length);
        String s = new String(ch, start, length);
        if ("hint".equals(currentName)) {
            hint = s;
        }
    }

    @Override
    public void endDocument() throws SAXException {
        // TODO Auto-generated method stub

        super.endDocument();
    }
}
